package com.faith.timefloating.myutils;

import com.faith.timefloating.listener.OnMyButtonEvents;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Button;

public class MyFlingButton extends Button  implements 
GestureDetector.OnGestureListener{
	private GestureDetectorCompat mDetector; 
	private final String DEBUG_TAG="MyButton";
	private volatile boolean isExpand=false;
	private OnMyButtonEvents events;
	public MyFlingButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mDetector = new GestureDetectorCompat(context, this);
	}
	
	public void setOnMyButtonEvents(OnMyButtonEvents events){
		this.events = events;
	}
	
	public MyFlingButton(Context context) {
		super(context);
		mDetector = new GestureDetectorCompat(context, this);
	}
	
	public void changeState(){
		isExpand = !isExpand;
	}
	public boolean isExpand(){
		return isExpand;
	}
	@Override
    public boolean onTouchEvent(MotionEvent event) {
		
		mDetector.onTouchEvent(event);      
        return super.onTouchEvent(event);
    }
	
	@Override
    public boolean onDown(MotionEvent event) { 
//        Log.e(DEBUG_TAG,"onDown: " + event.toString()); 
        return true;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, 
            float velocityX, float velocityY) {
//        Log.e(DEBUG_TAG, "onFling: velocityX-velocityY: "+velocityX+"-"+velocityY+" Details:" + event1.toString()+event2.toString());
    	if(events!=null){
    		events.onMyButtonFling(velocityX, velocityY);
    	}
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {
//        Log.e(DEBUG_TAG, "onLongPress: " + event.toString()); 
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
//        Log.e(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {
//        Log.e(DEBUG_TAG, "onShowPress: " + event.toString());
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
//        Log.e(DEBUG_TAG, "onSingleTapUp: " + event.toString());
        return true;
    }
}
